package com.lxr0860.lxr0860production_test.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesUtils {

    private static final String SPNAME_BASEINFO = "App_ConfigInfo";

    /**
     * 保存数据到文件
     * @param context
     * @param key
     * @param data
     */
    public static void saveData(Context context, String key, String data){
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(SPNAME_BASEINFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(key, data);
        edit.apply();
    }

    /**
     * 保存数据到文件
     * @param key
     * @param state
     */
    public static void saveBooleanData(Context context, String key, boolean state){
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(SPNAME_BASEINFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean(key, state);
        edit.apply();
    }

    /**
     * 删除保存的数据
     * @param key
     */
    public static void deleteData(Context context,String key){
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(SPNAME_BASEINFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.remove(key);
        edit.apply();
    }

    /**
     * 从文件中读取数据
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static String getData(Context context,String key, String defValue){
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(SPNAME_BASEINFO, Context.MODE_PRIVATE);
        return sharedPreferences.getString(key, defValue);
    }

    /**
     * 从文件中读取数据
     * @param context
     * @param key
     * @param defValue
     * @return
     */
    public static boolean getBooleanData(Context context,String key, boolean defValue){
        SharedPreferences sharedPreferences = context
                .getSharedPreferences(SPNAME_BASEINFO, Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, defValue);
    }
}
